package com.mathdroid.model.enums;

import java.util.LinkedList;

import com.mathdroid.exceptionhandling.ParserException;

/**
 * Each class gets an enumeration and name.
 */
public enum EClass {
	
	/**
	 * Trooper class.
	 */
	TROOPER("Trooper"),
	/**
	 * Smuggler class.
	 */
	SMUGGLER("Smuggler"),
	/**
	 * Jedi Knight class.
	 */
	JEDI_KNIGHT("Jedi Knight"),
	/**
	 * Jedi Consular class.
	 */
	JEDI_CONSULAR("Jedi Consular"),
	
	/**
	 * Bounty Hunter class.
	 */
	BOUNTY_HUNTER("Bounty Hunter"),
	/**
	 * Sith Warrior class.
	 */
	SITH_WARRIOR("Sith Warrior"),
	/**
	 * Imperial Agent class.
	 */
	IMPERIAL_AGENT("Imperial Agent"),
	/**
	 * Sith Inquisitor class.
	 */
	SITH_INQUISITOR("Sith Inquisitor");
	
	/**
	 * String representation of this enumeration.
	 */
	private String name = null;
	
	/**
	 * Creates an class enumeration.
	 * @param n Name.
	 */
	private EClass(final String n) {
		this.name = n;
	}
	
	@Override	
	public String toString() {
		return this.name;
	}

	/**
	 * Get all available classes as a list.
	 * @return All available classes.
	 */
	public static LinkedList<EClass> getAllClasses() {
		LinkedList<EClass> c = new LinkedList<EClass>();
		
		c.add(TROOPER);
		c.add(SMUGGLER);
		c.add(JEDI_KNIGHT);
		c.add(JEDI_CONSULAR);
		c.add(BOUNTY_HUNTER);
		c.add(SITH_WARRIOR);
		c.add(IMPERIAL_AGENT);
		c.add(SITH_INQUISITOR);
		
		return c;
	}
	
	/**
	 * Parse a string to an enumeration object. The parser compares the
	 * string representation of this enumeration with the parameter, not the
	 * literal.
	 * @param s String.
	 * @return EClass object.
	 * @throws ParserException The string cannot be parsed.
	 */
	public static final EClass parseEClass(final String s) 
	throws ParserException {
		EClass c = null;
		
		for (EClass cl : EClass.getAllClasses()) {
			if (s.equals(cl.toString())) {
				c = cl;
			}
		}
		
		if (c == null) {
			ParserException p = new ParserException("No such class: \"" 
					+ s + "\"");
			p.fillInStackTrace();
			throw p;
		}
		
		return c;
	}
}
